package com.i2bt.app.share.model;

import com.i2bt.common.AndroidRestContract;
import com.i2bt.common.UserConfig;

import com.i2bt.adapter.SharedFriendListAdapter;
import com.i2bt.app.BaseFragment;
import com.i2bt.app.BaseJsonModel;
import com.i2bt.app.share.ShareFragment;
import com.i2bt.bean.ShareBean;

import org.json.JSONException;
import org.json.JSONObject;

public class FriendShareJsonHandle extends BaseJsonModel {
	ShareFragment frg;
	ShareBean bean;

	public FriendShareJsonHandle(BaseFragment fragment) {
		super(fragment);
		// TODO Auto-generated constructor stub
		this.frg = (ShareFragment) fragment;
	}

	@Override
	protected String logicToWrap() throws JSONException {
		// TODO Auto-generated method stub
		this.bean = (ShareBean) this.beanParam;
		try {
			jsonParam.put(AndroidRestContract.ACTION, bean.getAct());
			jsonParam.put(AndroidRestContract.TOKEN, UserConfig.token);
			jsonParam.put(AndroidRestContract.SHARE_FOLDERS, bean.getPath());
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonParam.toString();
	}

	@Override
	protected void logicToUnwrap(boolean isSuc, String result)
			throws JSONException {
		// TODO Auto-generated method stub

		if (isSuc) {
			try {
				JSONObject ja = new JSONObject(result);
				boolean succ = ja.getBoolean(AndroidRestContract.SUCCEED);
				String mes = null;
				if (succ) {
					if (bean.getAct() == AndroidRestContract.DELETE_SHERE) {
						System.out.println("删除时:" + bean.getPosition());
						System.out.println(this.frg.friendSharedFilesList.get(
								bean.getPosition()).getFileName());
						frg.friendSharedFilesList.remove(bean.getPosition());
						frg.friendSharedListAdapter = new SharedFriendListAdapter(
								this.frg, frg.friendSharedFilesList);
						frg.friendSharedListView
								.setAdapter(this.frg.friendSharedListAdapter);
						this.frg.toastHandler("删除成功");
					} else if (bean.getAct() == AndroidRestContract.DOWNLOAD_SHARE) {
						// TODO
						download();
					}
				} else {
					mes = ja.getString(AndroidRestContract.MESSAGE);
					this.frg.toastHandler(succ + mes);
				}
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			this.frg.toastHandler("网络连接失败");
		}

	}

	public void download() {

	}
}
